from flask import session
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

import pymysql

from info.models import User

pymysql.install_as_MySQLdb()

# from flask import current_app

from info import create_app, db, models

app = create_app('dev')
manager = Manager(app)
# 将app与db关联
Migrate(app, db)
# 将迁移命令添加到manager中
manager.add_command('db', MigrateCommand)


# @app.route('/')
# def hello_world():
#     # session['name'] = "JKing"
#     return 'Hello World!'


# 集成创建管理员的函数到命令行
# python manage.py createsuperuser -n JKing -p jinxin1108
@manager.option('-n', '-name', dest="name")
@manager.option('-p', '-password', dest="password")
def createsuperuser(name, password):

    if not all([name, password]):
        print("参数不足")

    user = User()
    user.is_admin = True
    user.nick_name = name
    user.mobile = name
    user.password = password

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()

    print("添加成功")


if __name__ == '__main__':
    manager.run()
